package com.sinya.shoppingmall;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.sinya.shoppingmall.bean.AttributeBean;
import com.sinya.shoppingmall.bean.BaseResult;
import com.sinya.shoppingmall.bean.CartBean;
import com.sinya.shoppingmall.bean.ConsigneeBean;
import com.sinya.shoppingmall.bean.Constants;
import com.sinya.shoppingmall.bean.DeliveryBean;
import com.sinya.shoppingmall.bean.InvoiceBean;
import com.sinya.shoppingmall.bean.OrderBean;
import com.sinya.shoppingmall.bean.PayBean;
import com.sinya.shoppingmall.bean.ProductBean;
import com.sinya.shoppingmall.https.VolleyParams;
import com.sinya.shoppingmall.https.VolleyRequest;

import java.util.List;

public class OrderDetailActivity extends AppCompatActivity {

    LinearLayout ll_parent;

    TextView tv_mOrderId;
    TextView mOrderAddress;

    TextView mOrderStatus;
    TextView mDeliveryType;

    TextView mPayType;
    TextView mOrderCreatedDate;
    TextView mOrderDeliveryDate;

    TextView mOrderInvoiceIssued;
    TextView mOrderInvoiceHead;
    TextView mOrderDeliveryNeed;

    TextView mProductName;
    TextView mProductColor;
    TextView mProductSize;
    TextView mProductQuantity;
    TextView mProductPrice;
    ImageView mPicture;

    TextView mTotalQuantity;
    TextView mTotalFreight;
    TextView mOrderDiscount;
    TextView mAmountPaid;

//    TextView mCancelOrder;

    private String mUserid;
    private String mOrderId;

    public OrderBean orderInfo;
    public ConsigneeBean addressInfo;
    public PayBean payBean;
    public DeliveryBean deliveryInfo;
    public InvoiceBean invoiceInfo;
    public List<ProductBean> productList;
    public List<String> prom;
    public CartBean checkoutAddup;

    BaseResult bean;

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {

                case 0:
                    if (msg.obj != null) {

                        bean = (BaseResult) msg.obj;

                        orderInfo = bean.orderInfo;
                        addressInfo = bean.addressInfo;
                        payBean = bean.paymentInfo;
                        deliveryInfo = bean.deliveryInfo;
                        invoiceInfo = bean.invoiceInfo;
                        productList = bean.productList;
                        prom = bean.prom;
                        checkoutAddup = bean.checkoutAddup;

                        //显示数据
                        setData();
                    }

                    break;
                default:
                    break;
            }

            return true;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_order_message);

        initView();
        getBundleData();
        getDataFromNet();
    }

    public void initView() {

        ll_parent = (LinearLayout) findViewById(R.id.ll_parent);

        tv_mOrderId = (TextView) findViewById(R.id.orderlist_tv_orderId_content);
        mOrderAddress = (TextView) findViewById(R.id.orderlist_tv_orderaddress);

        mDeliveryType = (TextView) findViewById(R.id.tv_deliveryType);
        mOrderStatus = (TextView) findViewById(R.id.tv_orderstatus);

        mPayType = (TextView) findViewById(R.id.tv_payType);
        mOrderCreatedDate = (TextView) findViewById(R.id.tv_order_createDate);
        mOrderDeliveryDate = (TextView) findViewById(R.id.tv_deliveryDate);

      /*  mOrderInvoiceIssued = (TextView) findViewById(R.id.tv_invoiceissued);
        mOrderInvoiceHead = (TextView) findViewById(R.id.tv_invoiceHead_value);
        mOrderDeliveryNeed = (TextView) findViewById(R.id.tv_deliveryNeed_value);*/

        mProductName = (TextView) findViewById(R.id.tv_productName_value);
        mProductColor = (TextView) findViewById(R.id.tv_color_value);
        mProductSize = (TextView) findViewById(R.id.tv_size_value);
        mProductQuantity = (TextView) findViewById(R.id.tv_count_value);
        mProductPrice = (TextView) findViewById(R.id.tv_price_value);
        mPicture = (ImageView) findViewById(R.id.tv_picture);

        mTotalQuantity = (TextView) findViewById(R.id.tv_orderdetail_totalquantity);
        mTotalFreight = (TextView) findViewById(R.id.tv_totalFreight);
        mOrderDiscount = (TextView) findViewById(R.id.tv_orderdiscount);
        mAmountPaid = (TextView) findViewById(R.id.tv_amountpaid);

//        mCancelOrder = (TextView) findViewById(R.id.orderdetail_tv_cancelorder);

        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText("订单详情");

        ImageView back = (ImageView) findViewById(R.id.img_back);
        back.setImageResource(R.drawable.btn_title_back_selector);
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }


    private void getBundleData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            mUserid = bundle.getString("userid");
            mOrderId = bundle.getString("orderId");
        }
    }

    private void getDataFromNet() {

        VolleyRequest.Start(this, handler, 0, VolleyParams.orderDetail, new String[]{"20428", mOrderId});
    }

    private void setData() {

        if (bean.error_code == 1538) {
            ll_parent.setVisibility(View.GONE);
        }

        if (orderInfo == null || addressInfo == null || payBean == null || deliveryInfo == null || invoiceInfo == null ||
                productList == null || prom == null || checkoutAddup == null) {

            return;
        }

        mOrderAddress.setText("sinya");
        mOrderStatus.setText(orderInfo.status);
        tv_mOrderId.setText(mOrderId);

        String deliveryType = deliveryInfo.type;
        if (deliveryType.equals("1")) {
            deliveryType = this.getResources().getStringArray(R.array.delivery_arrays)[0];
        } else if (deliveryType.equals("2")) {
            deliveryType = this.getResources().getStringArray(R.array.delivery_arrays)[1];
        } else if (deliveryType.equals("3")) {
            deliveryType = this.getResources().getStringArray(R.array.delivery_arrays)[2];
        }
        mDeliveryType.setText(deliveryType);

        int type = payBean.type;
        String paymentType = null;
        if (type == 1) {
            paymentType = this.getResources().getStringArray(R.array.paytype_arrays)[0];
        } else if (type == 2) {
            paymentType = this.getResources().getStringArray(R.array.paytype_arrays)[1];
        } else if (type == 3) {
            paymentType = this.getResources().getStringArray(R.array.paytype_arrays)[2];
        }
        mPayType.setText(paymentType);

        mOrderCreatedDate.setText(orderInfo.time);
        mOrderDeliveryDate.setText(orderInfo.time);
//        mOrderInvoiceIssued.setText("sinya");
//        mOrderInvoiceHead.setText("sinya");

        ProductBean productBean = productList.get(0);
        String name = productBean.name;
        String price = productBean.price;
        mProductName.setText(name);
        mProductPrice.setText("$" + price);
        mProductQuantity.setText(8 + "");

        List<AttributeBean> productProperty = productBean.productProperty;
        if (productProperty != null) {
            AttributeBean colorAttributeBean = productProperty.get(0);
            AttributeBean sizeAttributeBean = productProperty.get(1);
            mProductColor.setText(colorAttributeBean.v);
            mProductSize.setText(sizeAttributeBean.v);
        }

        String url = productBean.pic;
        new BitmapUtils(this).display(mPicture, Constants.BASE_URL + url);

        String freight = checkoutAddup.freight;
        String totalPrice = checkoutAddup.totalPrice;
        String totalCount = checkoutAddup.totalCount;
        String totalPoint = checkoutAddup.totalPoint;
        mTotalFreight.setText("$" + freight);
        mTotalQuantity.setText(totalCount);
        mOrderDiscount.setText("$" + totalPoint);
        mAmountPaid.setText("$" + totalPrice);

    }



}
